home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / prolog_2.zip / EXPERT.ZIP / KNAPSACK.PRO < prev    next >
Text File  |  1987-05-26  |  3KB  |  79 lines

  1.         /*
  2.                 CS 475     Computer Science Language Lab. -- PROLOG
  3.  
  4.                 By John Sun
  5.  
  6.                                                                   */
  7.  
  8.         /*
  9.  
  10.                 Instruction for knapsack
  11.                                                                   */
  12.  
  13.  
  14.         knapsacks :- cls, introduction, 
  15.            get_data ( N_item, Total_volume, Total_weight ), 
  16.            solution ( Total_volume, Total_weight, 1 ), fail.
  17.         knapsacks :- exitsys.
  18.    
  19.         introduction :- 
  20.            print ('This game is to find the best item(s) to put into a '), 
  21.            print ('knapsack.' ), nl,
  22.            print ('First, I need to tell you something about this game.  ' ),
  23.            print ('This game is to fill the\n ' ), 
  24.            print ('knapsack with some items each ' ), 
  25.            print ('of them has a specific volume and weight. \n'),
  26.            print ('Next, you have to specify the volume and weight you want.'),
  27.            print ('\nThen I will figure out which item can fit in it. ' ),
  28.            print ('\nFinally, don\'t forget to '), 
  29.            print ('put a period after each input.' ), nl, nl, nl. 
  30.  
  31.         get_data ( N_item, Total_volume, Total_weight ) :-
  32.            get_number_item ( N_item ), 
  33.            get_volume ( Total_volume ), 
  34.            get_weight ( Total_weight ),
  35.            build_item ( N_item ).
  36.  
  37.         get_number_item ( N_item ) :-
  38.            nl, tab ( 10 ),
  39.            print ( 'Enter the numbers of ITEM you have : ' ),
  40.            read ( N_item ), asserta(max_item(N_item)), nl.
  41.  
  42.         get_volume ( Total_volume ) :- 
  43.            tab ( 10 ),
  44.            print ( 'Please enter the VOLUME you want : '),
  45.            read ( Total_volume ), nl.
  46.  
  47.         get_weight ( Total_weight ) :- 
  48.            tab ( 10 ),
  49.            print ( 'Please enter the WEIGHT you want : '),
  50.            read ( Total_weight ), nl.
  51.  
  52.         build_item ( 0 ).
  53.         build_item ( N_item ) :- 
  54.            N_item > 0, print ( '\n\nINPUT FOR THE ITEM ', N_item ),
  55.            print ( '\nTHE VOLUME: ' ), read ( Volume ),
  56.            print ( 'THE WEIGHT: ' ), read ( Weight ),
  57.            assertz ( item ( N_item, Volume, Weight ) ), 
  58.            Temp is N_item - 1,
  59.            build_item ( Temp ).
  60.  
  61.         solution ( 0, 0, _ ).
  62.         solution ( Target_volume, Target_weight, Candidate ) :- 
  63.            Target_volume > 0, Target_weight > 0, max_item(X), Candidate =< X, 
  64.            item ( Candidate, Volume, Weight ), 
  65.            print ( 'I am thinking!!\n' ),
  66.            Temp_1 is Target_volume - Volume, 
  67.            Temp_2 is Target_weight - Weight, 
  68.            Temp_3 is Candidate + 1,
  69.            solution ( Temp_1, Temp_2, Temp_3 ),
  70.            print ( '\n\nPicked item ', Candidate, ' with volume ', Volume, 
  71.                    ' unit(s) and weight ', Weight, ' unit(s).' ), nl.
  72.         solution ( Target_volume, Target_weight, Candidate ) :-
  73.            Target_volume > 0, Target_weight > 0, 
  74.            max_item(X), Candidate =< X,
  75.            Temp is Candidate + 1, 
  76.            solution ( Target_volume, Target_weight, Temp ).
  77.  
  78.         ?- knapsacks.
  79.